package com.andy.qi.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.andy.qi.conf.AccountConf;
import com.andy.qi.domain.Account;
import com.andy.qi.repository.AccountRepository;
import com.andy.qi.repository.util.StrUtils;

@CrossOrigin(origins = "*")
@RestController
public class AccountController extends WebApi {
    
//    String HALL_IP = "127.0.0.1";
//    int HALL_CLIENT_PORT = 9001;
//    int HALL_ROOM_PORT = 9002;
//    String VERSION = "20161227";
//    String APP_WEB = "http://fir.im/2f17";
//    String hallAddr = HALL_IP  + ":" + HALL_CLIENT_PORT;
//    String ACCOUNT_PRI_KEY = "^&*#$%()@";
    
    @Autowired private AccountRepository accountRepository;
    @Autowired private AccountConf conf;
    
    @RequestMapping("/register")
    Object register(String account, String password) {
        if(accountRepository.exists(account)) {
            return fail("account has been used.");
        }
        accountRepository.save(new Account(account, password));
        return succ();
    }
    @RequestMapping("/get_version")
    Object getVersion() {
        Map<String, Object> ret = result();
        ret.put("version", conf.getVersion());
        return succ(ret);
    }
    @RequestMapping("/get_serverinfo")
    Object getServerinfo() {
        Map<String, Object> ret = result();
        ret.put("version", conf.getVersion());
        ret.put("hall", conf.getHallAddr());
        ret.put("appweb", conf.getAppWeb());
        return succ(ret);
    }
    
    @RequestMapping("/guest")
    Object guest(HttpServletRequest request, String account) {
        String accStr = "guest_" + account;
        String sign = StrUtils.md5(accStr + ip(request) + conf.getAccountPriKey());
        Map<String, Object> ret = result();
        ret.put("account", accStr);
        ret.put("halladdr", conf.getHallAddr());
        ret.put("sign", sign);
        return succ(ret);
    }
    @RequestMapping("/auth")
    Object auth(HttpServletRequest request, String account, String password) {
        Account acc = accountRepository.findOne(account);
        
        if(!acc.getPassword().equals(password)) {
            return fail("invalid account");
        }
        String accStr = "vivi_" + account;
        String sign = StrUtils.md5(accStr + ip(request) + conf.getAccountPriKey());
        
        Map<String, Object> ret = result();
        ret.put("account", accStr);
        ret.put("sign", sign);
        return succ(ret);
    }
    

//    var appInfo = {
//        Android:{
//            appid:"wxe39f08522d35c80c",
//            secret:"fa88e3a3ca5a11b06499902cea4b9c01",
//        },
//        iOS:{
//            appid:"wxcb508816c5c4e2a4",
//            secret:"7de38489ede63089269e3410d5905038",      
//        }
//    };
//
//    function get_access_token(code,os,callback){
//        var info = appInfo[os];
//        if(info == null){
//            callback(false,null);
//        }
//        var data = {
//            appid:info.appid,
//            secret:info.secret,
//            code:code,
//            grant_type:"authorization_code"
//        };
//
//        http.get2("https://api.weixin.qq.com/sns/oauth2/access_token",data,callback,true);
//    }
//
//    function get_state_info(access_token,openid,callback){
//        var data = {
//            access_token:access_token,
//            openid:openid
//        };
//
//        http.get2("https://api.weixin.qq.com/sns/userinfo",data,callback,true);
//    }
//
//    function create_user(account,name,sex,headimgurl,callback){
//        var coins = 1000;
//        var gems = 21;
//        db.is_user_exist(account,function(ret){
//            if(!ret){
//                db.create_user(account,name,coins,gems,sex,headimgurl,function(ret){
//                    callback();
//                });
//            }
//            else{
//                db.update_user_info(account,name,headimgurl,sex,function(ret){
//                    callback();
//                });
//            }
//        });
//    };
//    app.get('/wechat_auth',function(req,res){
//        var code = req.query.code;
//        var os = req.query.os;
//        if(code == null || code == "" || os == null || os == ""){
//            return;
//        }
//        console.log(os);
//        get_access_token(code,os,function(suc,data){
//            if(suc){
//                var access_token = data.access_token;
//                var openid = data.openid;
//                get_state_info(access_token,openid,function(suc2,data2){
//                    if(suc2){
//                        var openid = data2.openid;
//                        var nickname = data2.nickname;
//                        var sex = data2.sex;
//                        var headimgurl = data2.headimgurl;
//                        var account = "wx_" + openid;
//                        create_user(account,nickname,sex,headimgurl,function(){
//                            var sign = crypto.md5(account + req.ip + config.ACCOUNT_PRI_KEY);
//                            var ret = {
//                                errcode:0,
//                                errmsg:"ok",
//                                account:account,
//                                halladdr:hallAddr,
//                                sign:sign
//                            };
//                            send(res,ret);
//                        });                     
//                    }
//                });
//            }
//            else{
//                send(res,{errcode:-1,errmsg:"unkown err."});
//            }
//        });
//    });
//
//    app.get('/base_info',function(req,res){
//        var userid = req.query.userid;
//        db.get_user_base_info(userid,function(data){
//            var ret = {
//                errcode:0,
//                errmsg:"ok",
//                name:data.name,
//                sex:data.sex,
//                headimgurl:data.headimg
//            };
//            send(res,ret);
//        });
//    });
    
}
